﻿$(document).ready(function () {
    ko.applyBindings(viewModel);
    viewModel.loadFeeds();
});

//ViewModel
function feedViewModel(feed) {
    var feedVM =
            {
                Feed: feed
                , FeedId: ko.observable(feed.FeedId)
                , Name: ko.observable(feed.Name)
                , Url: ko.observable(feed.Url)
                , Latitude: ko.observable(feed.Latitude)
                , Longitude: ko.observable(feed.Longitude)
                , IsDeleted: ko.observable(false)
                , Visits: ko.observableArray([])
                , Items: ko.observableArray([])

                , deleteFeed: function () {
                    this.IsDeleted(true);
                }
                , cancel: function () {
                    this.Name(this.Feed.Name);
                    this.Url(this.Feed.Url);
                    this.Latitude(this.Feed.Latitude);
                    this.Longitude(this.Feed.Longitude);
                    this.IsDeleted(false);
                }

                , getFeed: function () {
                    var feed = new mal.balades.Business.DataModel.Feed();

                    feed.FeedId = this.FeedId();
                    feed.Name = this.Name();
                    feed.Url = this.Url();
                    feed.Latitude = this.Latitude();
                    feed.Longitude = this.Longitude();
                    feed.IsDeleted = this.IsDeleted()
                    return feed;
                }

                
            };

    feedVM.IsDirty = ko.dependentObservable(function () {
        if (this.Feed.Name != this.Name())
            return true;
        if (this.Feed.Url != this.Url())
            return true;
        if (this.Feed.Latitude != this.Latitude())
            return true;
        if (this.Feed.Longitude != this.Longitude())
            return true;
        if (this.IsDeleted())
            return true;
        return false;
    }, feedVM);

    return feedVM;

}

var viewModel =
         {
             feedName: ko.observable()
            , feedUrl: ko.observable()
            , feedLatitude: ko.observable()
            , feedLongitude: ko.observable()
            , feeds: ko.observableArray([])

            , loadFeeds: function () {
                var service = new mal.balades.Web.Administration.FeedService();
                service.GetAllFeeds(viewModel.onGetAllFeedsSuccess, null, null);
            }
            , onGetAllFeedsSuccess: function (result) {

                viewModel.feeds([]);
                for (var i = 0; i < result.length; i++) {
                    viewModel.feeds.push(new feedViewModel(result[i]));
                }
            }

             , saveFeeds: function () {
                 var service = new mal.balades.Web.Administration.FeedService();

                 var dirtyFeeds = [];

                 for (var i = 0; i < this.feeds().length; i++) {
                     var feedVM = this.feeds()[i];
                     var feed;

                     if (feedVM.IsDirty()) {
                         feed = feedVM.getFeed();

                         dirtyFeeds.push(feed);
                     }
                 }
                 service.SaveFeeds(dirtyFeeds, this.onSaveFeedsSuccess, this.onSaveFeedsFail, null);
             }
            , onSaveFeedsSuccess: function (result) {
                viewModel.onGetAllFeedsSuccess(result);
            }
            , onSaveFeedsFail: function (result) {
                alert("Erreur");
            }

            ,
             //Add a new feed
             addFeed: function () {
                 var feed = new feedViewModel(new mal.balades.Business.DataModel.Feed());
                 feed.Name(this.feedName());
                 feed.Url(this.feedUrl());
                 feed.Latitude(this.feedLatitude());
                 feed.Longitude(this.feedLongitude());

                 viewModel.feeds.push(feed);

                 this.resetFeed();
             }

               , resetFeed: function () {
                   this.feedName("");
                   this.feedUrl("");
                   this.feedLatitude("");
                   this.feedLongitude("");
               }
         };